﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameBasicsLibrary.Contracts;
using GameBasicsLibrary.Constraining;

namespace GameBasicsLibrary
{
    public class FirstPersonPlayer:ITransformable
    {
        public Vector3 Position{get; set;}
        public Vector3 Rotation { get; set; }
        public Vector3 Scale { get; set; }
        private List<ChildTransform> Children;
        public Vector3 Forward
        {
            get
            {
                return calculateForward(Rotation);
            }
            
        }
        public TargetCamera playerCam { get; private set;}
        public Vector3 camOffset;
        public Model playerShape;

        public FirstPersonPlayer(Vector3 Postition, Vector3 Rotation, TargetCamera playerCam, Vector3 camOffset)
        {
            this.Position = Postition;
            this.Rotation = Rotation;
            this.playerCam = playerCam;
            this.camOffset = camOffset;
            playerCam.Position = Position + camOffset;
            //incializo el children
            Children = new List<ChildTransform>();
        }
        public void AddChildTransform(CModel model, Vector3 OffsetPosition){
            Children.Add(new ChildTransform(this, OffsetPosition, model));
        }
        public ChildTransform GetChildTransform(int i)
        {   
                return Children[i];
        }
        
        public void Update()
        {
            #region updateCam

            playerCam.Position = this.Position + camOffset;
            playerCam.Target = playerCam.Position + (Vector3.Negate(this.Forward) * 5);
            playerCam.View = Matrix.CreateLookAt(playerCam.Position, playerCam.Target, Vector3.Up);
            playerCam.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), playerCam.graphicsDevice.Viewport.AspectRatio, 0.1f, 10000.0f);

            foreach (ChildTransform child in Children)
            {
                child.Update();
                //guarrada, sobre escribo la rotaccion 15 grados para que se vea que son manos
                //Vector3 newRot = child.Rotation;
                //newRot = new Vector3(child.Rotation.X, child.Rotation.Y, 15);
                //child.ChildModel.Rotation = newRot;
            }
            #endregion updateCam
        }
        public void Draw()
        {
            foreach (ChildTransform child in Children)
            {
                child.Draw(this.playerCam.View, this.playerCam.Projection);
            }

        }

        public Vector3 calculateForward(Vector3 rotation)
        {
            Matrix mtxRot = Matrix.CreateFromYawPitchRoll(MathHelper.ToRadians(rotation.Y), MathHelper.ToRadians(rotation.X), MathHelper.ToRadians(rotation.Z));
            return Vector3.Transform(Vector3.Forward, mtxRot);
        }
    }
}
